package com.dbq.fast.business.ypage;

import com.dbq.fast.business.login.api.LoginApi;
import com.dbq.fast.business.product.api.CommonTransformer;
import com.dbq.fast.common.bean.User;
import com.dbq.fast.common.net.HttpManager;
import com.dbq.fast.common.net.RemoteApiCallBack;

import java.util.HashMap;

/**
 * Created by yuanchen on 2018/5/4.
 */

public class YPageApi {

    private static HttpManager httpManager = HttpManager.getInstance();

    public static void queryYData(int page, RemoteApiCallBack<YPageBean> c) {
        String token = User.getInstance().getToken();
        String vpnId = User.getInstance().getVpnId();
        HashMap body = new HashMap();
        body.put("vpnId", vpnId);
        body.put("page", page);
        body.put("size", 10);
        httpManager.getHttpService()
                .queryYData(token, body)
                .compose(new CommonTransformer<>())
                .subscribe(rewardInfoHttpResult -> {
                    if (rewardInfoHttpResult.isSuccess()) {
                        c.onSuccess(rewardInfoHttpResult.getData());
                    } else if(rewardInfoHttpResult.getCode() == 401){
                        new Thread(()->{
                            String newToken = LoginApi.refreshTokenSync(true);
                            if ("".equals(newToken)) return;
                            queryYData(page, c);
                        }).start();

                    } else {

                    }
                }, throwable -> {

                });
    }

}
